import json
import urllib.request
import urllib


def getUrl(url):
    req = urllib.request.Request(url)
    re = urllib.request.urlopen(req)
    str2 = re.read().decode("utf-8")
    js = json.loads(str2)
    return js


def getData(url):
    js = getUrl(url)
    for i in range(10):
        if js['data']['data'][i] is None:
            break
        resu = js['data']['data'][i]['title']
        prob = js['data']['data'][i]['content'][0]['value']
        writeFile("公告标题：" + resu)
        writeFile("公告详情：" + prob)
        writeFile("\n"+"\n")


def writeFile(data):
    with open(r"E:\filework\e.txt", "a", encoding="utf-8") as f:
        f.write(data + "\n")
        f.close()


url = "https://api.goingtowin.com/v1/government/notice/index?" \
      "start_at=&end_at=&page=%d&platform=pc&city_id=330700&token=6Tgpa5j5Mr7vVCJrND"

for num in range(1, 3):
    getData(url % num)






